import Fetcher from "@zxy-cn/fetcher-taro"
import authorizationStore from "../store/AuthorizationStore";
import {preHandleObjectWithOffsetDateTimeParser} from "../utils/Date";

let baseUrl = process.env.TARO_APP_SERVER_API!!;

const fetcher = new Fetcher(baseUrl)
fetcher.addRequestInterceptor(request => {
  let authorization = authorizationStore.authorization
  if (authorization) {
    request.headers["Authorization"] = authorization
  }
  return request
})

fetcher.addResponseInterceptor(response => {
  const data= response.data as any
  preHandleObjectWithOffsetDateTimeParser(data)
})

fetcher.addErrorInterceptor(error => {
  if (typeof error !== "string" && (error.status === 401 || error.status===403)) {
    // userStore.logOut() TODO
    authorizationStore.setAuthorization(null)
    // wechatLogIn()
  }
})

export default fetcher
